﻿Imports System.ComponentModel
Imports SIGI.Common.BE

Public Class UC_ABMProperties
#Region "<< -Attributes- >>"
    Private Enum Filter
        <Description("Propietario")>
        Owner
        <Description("Ubicación")>
        Location
        <Description("Nombre de Propiedad")>
        PropertyName
    End Enum

    Dim _PropertyController As PropertyController = Nothing
    Dim _Dt As DataTable = Nothing
    Dim _Property As PropertyBE = Nothing

#End Region

#Region "<< -Events- >>"
    Private Sub UC_ABMProperties_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            If Me.DesignMode Then Return

            btnSave.Visible = False
            btnCancel.Visible = False

            _PropertyController = New PropertyController

            LoadFilters()
            LoadGrid(True)
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnNew.Click
        Dim wFrm As frmNewProperty = Nothing
        Dim wLstProperty As List(Of PropertyBE) = Nothing

        Try
            _Behavior = Common.CommonDefinitions.CRUD.Insert
            wFrm = New frmNewProperty(True) With {.Behavior = Common.CommonDefinitions.CRUD.Insert}
            wFrm.ShowDialog()

            If wFrm.OperationResult = frmNewProperty.OperationResults.Save Then
                wLstProperty = wFrm.Properties
                _PropertyController.InsertBatch(wLstProperty)
                _MsgBox.ShowMessage("Nueva Propiedad/es", "Propiedades Creadas", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
                LoadGrid(True)
            End If

        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try

    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Dim wRowIdx As Integer = 0
        Dim wPropertyId As Integer = 0
        Dim wInputBox As frmInputVisorBox = Nothing
        Dim wMotive As String = String.Empty
        Dim wProperty As PropertyBE = Nothing

        Try
            If chkNotActiveProperties.Checked Then Return

            If _MsgBox.ShowMessage("Eliminar Propiedad", "¿Está seguro de que desea Eliminar la Propiedad?",
                                frmMessage.MessageType.Question_, frmMessage.MessageButtons.OkCancel) = frmMessage.MsgResult.Cancel Then
                Return
            End If
            wInputBox = New frmInputVisorBox
            wMotive = wInputBox.ShowBox("Baja de Propietario", frmInputVisorBox.Behavior.Write, "Cese de Servicio")

            wRowIdx = GridView1.FocusedRowHandle
            wPropertyId = _Dt.Rows(wRowIdx)("Id")

            wProperty = New PropertyBE With {.Id = wPropertyId, .AccountCloseMotive = wMotive}
            _PropertyController.Delete(wProperty)

            _MsgBox.ShowMessage("Eliminar Propiedad", "Propiedad Eliminada Exitosamente",
                                frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)

            'Refresco la grilla
            LoadGrid(True)

        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub chkNotActiveProperties_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkNotActiveProperties.CheckedChanged
        Try
            LoadGrid(Not chkNotActiveProperties.Checked)
            If chkNotActiveProperties.Checked Then
                btnDelete.Enabled = False
                btnUpdate.Enabled = False
            Else
                btnDelete.Enabled = True
                btnUpdate.Enabled = True
            End If
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
        Dim wProperty As PropertyBE = Nothing
        Dim wLstProperty As List(Of PropertyBE) = Nothing
        Dim wRowIdx As Integer = 0
        Dim wFrm As frmNewProperty = Nothing

        Try
            If chkNotActiveProperties.Checked Then Return

            _Behavior = Common.CommonDefinitions.CRUD.Update

            wRowIdx = GridView1.FocusedRowHandle
            wProperty = _PropertyController.SearchByPropertyId(_Dt.Rows(wRowIdx)("Id"))

            wLstProperty = New List(Of PropertyBE)
            wLstProperty.Add(wProperty)
            wFrm = New frmNewProperty(False, wLstProperty) With {.Behavior = Common.CommonDefinitions.CRUD.Update}
            wFrm.ShowDialog()

            If wFrm.OperationResult = frmNewProperty.OperationResults.Save Then
                _Property = wFrm.Properties(0)
                _Property.Id = _Dt.Rows(GridView1.FocusedRowHandle)("Id")
                _PropertyController.Update(_Property)
                _MsgBox.ShowMessage("Actualización", "Datos Actualizados", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
            End If

        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Try
            If String.IsNullOrEmpty(txtFilter.Text) Then Return

            _PropertyController = New PropertyController
            Select Case cmbFilterType.Text
                Case GenericMethods.GetEnumItemDescription(Filter.Owner)
                    _Dt = _PropertyController.SearchByFilters(Nothing, txtFilter.Text, String.Empty, String.Empty, Not chkNotActiveProperties.Checked)
                Case GenericMethods.GetEnumItemDescription(Filter.Location)
                    _Dt = _PropertyController.SearchByFilters(Nothing, String.Empty, txtFilter.Text, String.Empty, Not chkNotActiveProperties.Checked)
                Case GenericMethods.GetEnumItemDescription(Filter.PropertyName)
                    _Dt = _PropertyController.SearchByFilters(Nothing, String.Empty, String.Empty, txtFilter.Text, Not chkNotActiveProperties.Checked)
                Case Else

            End Select

            BindingSource1.DataSource = _Dt
            BindingSource1.ResetBindings(True)
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnRemoveFilter_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveFilter.Click
        Try
            LoadGrid(Not chkNotActiveProperties.Checked)
            txtFilter.Text = String.Empty
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub
#End Region

#Region "<< -Methods- >>"
    Private Sub LoadFilters()
        Dim wFilters As String() = [Enum].GetNames(GetType(Filter))

        If wFilters Is Nothing OrElse wFilters.Length = 0 Then Return
        For Each wFilter As String In wFilters
            cmbFilterType.Items.Add(GenericMethods.GetEnumItemDescription([Enum].Parse(GetType(Filter), wFilter)))
        Next

        cmbFilterType.SelectedIndex = 0
    End Sub

    Private Sub LoadGrid(pActive As Boolean)
        _Dt = _PropertyController.SearchByFilters(Nothing, String.Empty, String.Empty, String.Empty, pActive)

        BindingSource1.DataSource = _Dt
        BindingSource1.ResetBindings(True)

        If chkNotActiveProperties.Checked Then
            GridView1.Columns("Estado").Visible = False
            GridView1.Columns("Motivo_Baja").Visible = True
        Else
            GridView1.Columns("Estado").Visible = True
            GridView1.Columns("Motivo_Baja").Visible = False
        End If
    End Sub

#End Region

End Class
